﻿using System;
using UnityEditor;
using UnityEditor.Build.Reporting;
using UnityEngine;

namespace SCore.Editor.BuildPackage.Player
{
    public class BuildPlayer
    {
        private static readonly string[] DefaultScenes = new string[]
        {
            "Assets/Boot/Boot.unity",
        };

        public static void SBuild()
        {
            var sBuildChannel = BuildPipelineConfig.Instance._buildChannel;
            var sBuildTarget = BuildPipelineConfig.Instance._buildTarget;
            if (BuildPipelineConfig.Instance._isExportAAb)
            {
                $"[Auto]======aab build: {sBuildChannel.GetBuildTarget()}, {sBuildTarget._buildOptions}, {BuildPipelineConfig.Instance.GetOutPutPath()[0]}"
                    .Log();
                BuildAAb();
            }
            else
            {
                $"[Auto]======apk build: {sBuildChannel.GetBuildTarget()}, {sBuildTarget._buildOptions}, {BuildPipelineConfig.Instance.GetOutPutPath()[0]}"
                    .Log();
                Build();
            }
        }

        private static void BuildAAb()
        {
            var sBuildChannel = BuildPipelineConfig.Instance._buildChannel;
            var sBuildTarget = BuildPipelineConfig.Instance._buildTarget;


            var bpOptions = new BuildPlayerOptions();
            bpOptions.scenes = DefaultScenes;
            bpOptions.target = sBuildChannel.GetBuildTarget();
            bpOptions.options = sBuildTarget._buildOptions; //BuildOptions.None; 
            bpOptions.locationPathName = BuildPipelineConfig.Instance.GetOutPutPath()[0];
            bpOptions.targetGroup = sBuildChannel.GetBuildTargetGroup();
            // AssetPackConfig assetPackConfig = new AssetPackConfig();
            // assetPackConfig.SplitBaseModuleAssets = true;
            // bool result = AppBundlePublisher.Build(bpOptions, assetPackConfig, false);
            // if (!result)
            // {
            //     throw new Exception();
            // }
        }

        private static void Build()
        {
            var sBuildChannel = BuildPipelineConfig.Instance._buildChannel;
            var sBuildTarget = BuildPipelineConfig.Instance._buildTarget;

            ("XXXXXXX========= Build " + BuildPipelineConfig.Instance.GetOutPutPath()[0]).Log();
            var report = UnityEditor.BuildPipeline.BuildPlayer(DefaultScenes,
                BuildPipelineConfig.Instance.GetOutPutPath()[0], sBuildChannel.GetBuildTarget(),
                sBuildTarget._buildOptions);
            if (report.summary.result != BuildResult.Succeeded)
            {
                // yy added: print build detail
                foreach (var item in report.steps)
                    if (item.messages.Length > 0)
                    {
                        (item.name + " has errors:").Error();
                        foreach (var message in item.messages)
                            if (message.type == LogType.Error
                                || message.type == LogType.Assert
                                || message.type == LogType.Exception)
                                Debug.LogError($"===build_error: {item.name} {message.content} {message.type}");
                    }

                Debug.LogError(report.summary.result);
                throw new Exception();
            }
        }
    }
}